package handler

import (
	"chat_oa/api/request"
	"chat_oa/internal/dao"
	"chat_oa/internal/model"
	"github.com/gin-gonic/gin"
	"net/http"
)

func AddUser(c *gin.Context) {
	var req request.AddUserReq
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(200, gin.H{
			"code":    10000,
			"message": "参数错误",
			"data":    nil,
		})
		return
	}
	var user model.User
	dao.GetOneByFields(&model.User{Mobile: req.Mobile}, &user)
	if user.Id > 0 {
		c.JSON(200, gin.H{
			"code":    10000,
			"message": "该员工已存在，禁止添加",
			"data":    nil,
		})
		return
	}
	user = model.User{
		Mobile:   req.Mobile,
		Password: req.Password,
	}
	result := dao.Create(&user)
	if !result {
		c.JSON(200, gin.H{
			"code":    10000,
			"message": "员工添加失败",
			"data":    false,
		})
		return
	}
	c.JSON(200, gin.H{
		"code":    http.StatusOK,
		"message": "员工添加成功",
		"data":    true,
	})
}

func GetOnlineUser(c *gin.Context) {
	var result []model.User
	for userId, _ := range OnlineUser {
		var user model.User
		dao.GetOneById(userId, &user)
		if user.Id == 0 {
			continue
		}
		result = append(result, user)
	}
}
